import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse, Arc

# 支持中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
# 支持负数
plt.rcParams['axes.unicode_minus'] = False

# 设置图形
fig, ax = plt.subplots(figsize=(10, 8))

# 椭圆参数
a = 3
b = 2
c = np.sqrt(a**2 - b**2)

# 绘制椭圆
theta = np.linspace(0, 2*np.pi, 100)
x_ellipse = a * np.cos(theta)
y_ellipse = b * np.sin(theta)
ax.plot(x_ellipse, y_ellipse, 'b-', label=f'椭圆: $x^2/{a}^2 + y^2/{b}^2 = 1$')

# 焦点
ax.plot(-c, 0, 'ro', markersize=8, label='焦点 $F_1$')
ax.plot(c, 0, 'ro', markersize=8, label='焦点 $F_2$')
ax.text(-c-0.3, 0.2, '$F_1$', fontsize=12)
ax.text(c+0.1, 0.2, '$F_2$', fontsize=12)

# 椭圆上一点P
theta_p = np.pi/3
x_p = a * np.cos(theta_p)
y_p = b * np.sin(theta_p)
ax.plot(x_p, y_p, 'go', markersize=8, label='点P')
ax.text(x_p+0.1, y_p+0.1, 'P', fontsize=12)

# 外角平分线及相关点
# 简化示例，实际需要复杂几何计算
x_r = 2.5
y_r = 1.5
ax.plot(x_r, y_r, 'mo', markersize=8, label='点R')
ax.text(x_r+0.1, y_r+0.1, 'R', fontsize=12)

# 轨迹圆（R点的轨迹）
theta_circle = np.linspace(0, 2*np.pi, 100)
x_circle = a * np.cos(theta_circle)
y_circle = a * np.sin(theta_circle)
ax.plot(x_circle, y_circle, 'r--', label='R点轨迹: $x^2+y^2=a^2$')

# 设置图形属性
ax.set_xlim(-4, 4)
ax.set_ylim(-3, 3)
ax.set_aspect('equal')
ax.grid(True, alpha=0.3)
ax.axhline(y=0, color='k', linewidth=0.5)
ax.axvline(x=0, color='k', linewidth=0.5)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('例题3: 椭圆焦点相关轨迹问题')
ax.legend()

plt.tight_layout()
plt.show()